package org.brownflat.lighthouse.monitoring.ci.cruisecontrol;

import org.brownflat.lighthouse.monitoring.ci.Builder;
import org.brownflat.lighthouse.monitoring.ci.ConnectionException;
import org.brownflat.lighthouse.monitoring.ci.Feed;
import org.brownflat.lighthouse.monitoring.ci.HTTPServer;
import org.brownflat.lighthouse.monitoring.ci.Monitorable;

public class CruiseControlServer extends HTTPServer implements Monitorable {

    private Builder<Feed, String> builder;

    public CruiseControlServer(String host, int port, Builder<Feed, String> builder) {
        super(host, port);
        this.builder = builder;
    }

    @Override
    public boolean isCurrentlyBroken() {
        try {
            String result = super.execute(new CruiseControlLatestBuildsRequest());
            Feed feed = builder.build(result);
            return feed.isCurrentlyBroken();
        } catch (ConnectionException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getName() {
        return "CruiseControl";
    }

}
